#include "Pch.h"

#include "../Core/RayTrace/SphericalSamplers.h"
#include "../Core/Math/SphericalHarmonics.h"
#include "../Core/Math/Common.h"
#include "../Core/Application.h"

#include "GlVisualizer.h"

#include <vector>

using namespace IS;

namespace TestSphericalHarmonics
{

class TestApp : public Application
{
public:
	std::vector<Vec3f> mRandSamples1;
	std::vector<Vec3f> mRandSamples2;
	std::vector<Vec3f> mHaltonSamples1;

	TestApp()
		: Application( L"title=TestSphericalHarmonics;width=800;height=600;FSAA=1" )
	{
		mCamera.moveForward(-10.0f);

		const size_t cSampCnt = 256;

		{
			RandomSphericalSampler s;
			s.generateN(cSampCnt, mRandSamples1);
		}

        {
			RandomSphericalSamplerUniform s;
			s.generateN(cSampCnt, mRandSamples2);
		}

		{
			HaltonSphericalSampler s;
			s.generateN(cSampCnt, mHaltonSamples1);
		}
	}

	sal_override void update(float deltaTime)
	{
		// visualize the samples
		//std::vector<Vec3f>* samples = &mRandSamples1;
		//std::vector<Vec3f>* samples = &mRandSamples2;
		std::vector<Vec3f>* samples = &mHaltonSamples1;

		const Vec3f cOrig(0.0f, 2.0f, 0.0f);
		
		glPushAttrib(GL_LIGHTING_BIT|GL_DEPTH_BUFFER_BIT);
		glDisable(GL_DEPTH_TEST);
		glDisable(GL_LIGHTING);

		glBegin(GL_LINES);

		const float sz = 0.1f;
		const float radius = 3.0f;

		const size_t cSampCnt = samples->size();
		for(size_t isamp = 0; isamp < cSampCnt; ++isamp)
		{
			const Vec3f& samp = (*samples)[isamp];

			glColor3f(1.0f, 0.0f, 0.0f);
			GlVisualizer::drawLine(cOrig, cOrig + samp * radius);
			GlVisualizer::drawPoint(cOrig + samp * radius, sz);
		}

		glEnd();

		glPopAttrib();
	}

};	// class TestApp

}	// namespace TestSphericalHarmonics

class SHProjector // SHEvaluator
{
public:
	void project(int L, double theta, double phi, std_vector(double)& outCoeff);
};

void SHProjector::project(int L, double theta, double phi, std_vector(double)& outCoeff)
{
	for(int l = 0; l < L; ++l)
		for(int m = -l; m <= l; ++m)
			outCoeff.push_back(SphericalHarmonics::Y(l, m, theta, phi));
}

TEST(TestSphericalHarmonics)
{
	//TestSphericalHarmonics::TestApp app;
	//app.mainLoop();
}